package com.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CreateSession {
    public static final String connect = "49.235.88.227:2181";
    private static final CountDownLatch COUNT = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ZooKeeper zooKeeper = new ZooKeeper(connect, 5000, event->{
            if(event.getState() == Watcher.Event.KeeperState.SyncConnected){
                COUNT.countDown();
            }
            if(event.getType() == Watcher.Event.EventType.NodeDataChanged){
                System.out.println(event.getPath());
            }
        });
        COUNT.await(3000, TimeUnit.SECONDS);
        System.out.println(zooKeeper.getState());
//        zooKeeper.create("/aavvv1", "vv".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER);
        Stat st = new Stat();
        byte[] data = zooKeeper.getData("/aavvv1", true, st);
        System.out.println(new String(data));
        System.out.println(st);
        zooKeeper.setData("/aavvv1", "vvv".getBytes(), -1);
    }
}
